package org.nabc.core.creator;

import org.nabc.configuration.FileConfiguration;
import org.nabc.generate.AbstractRunnableTask;
import org.nabc.core.builder.TemplateGenerateTaskBuilder;
import org.nabc.support.PropertyValues;
import org.nabc.support.TemplateGenerateTaskValue;

import java.io.IOException;
import java.util.*;

public class TemplateGenerateTaskCreator {
    private TemplateGenerateTaskBuilder templateGenerateTaskBuilder;

    public TemplateGenerateTaskCreator(TemplateGenerateTaskBuilder templateGenerateTaskBuilder) {
        this.templateGenerateTaskBuilder = templateGenerateTaskBuilder;
    }

    public List<AbstractRunnableTask> creator(FileConfiguration fileConfiguration, PropertyValues propertyValues) throws IOException {
        List<AbstractRunnableTask> list = new ArrayList<>();
        Map<String, AbstractRunnableTask> map = new HashMap<>();
        ServiceLoader<AbstractRunnableTask> templateGenerateTasks = ServiceLoader.load(AbstractRunnableTask.class);

        for (AbstractRunnableTask templateGenerateTask : templateGenerateTasks) {
            templateGenerateTask.setModelAndType();
            templateGenerateTask.setParameters(fileConfiguration, propertyValues);
            map.put(templateGenerateTask.getModelAndType(), templateGenerateTask);
        }

        for (TemplateGenerateTaskValue templateGenerateTaskValue : templateGenerateTaskBuilder.getList()) {
            list.add(map.get(templateGenerateTaskValue.getModelAndType()));
        }

        return list;
    }
}
